package fun.werfamily.wf.user.core.controller;

import fun.werfamily.wf.base.annotation.MustLogin;
import fun.werfamily.wf.base.dto.LoginUserContext;
import fun.werfamily.wf.base.vo.Result;
import fun.werfamily.wf.user.core.domain.dto.req.UserInfoReqDTO;
import fun.werfamily.wf.user.core.domain.dto.req.UserRegisterReqDTO;
import fun.werfamily.wf.user.core.domain.dto.req.UserResetPwdReqDTO;
import fun.werfamily.wf.user.core.domain.dto.req.VerifyCodeReqDTO;
import fun.werfamily.wf.user.core.domain.dto.resp.UserInfoDTO;
import fun.werfamily.wf.user.core.domain.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: Mr.WenMing Zhang
 * @Date: 2022/07/28 14:44
 * @Description:
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
    @Resource
    private IUserService userService;

    @ApiOperation("注册")
    @PostMapping("/register")
    @MustLogin(value = false)
    public Result<UserInfoDTO> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return Result.success(userService.register(userRegisterReqDTO));
    }

    @ApiOperation("发送手机验证码")
    @MustLogin(value = false)
    @PostMapping("/sendVerifyCode")
    public Result<Boolean> sendVerifyCode(@Validated @RequestBody VerifyCodeReqDTO verifyCodeReqDTO) {
        return Result.success(userService.sendVerifyCode(verifyCodeReqDTO));
    }

    @ApiOperation("手机验证码校验")
    @PostMapping("/checkVerifyCode")
    @MustLogin(value = false)
    public Result<Boolean> checkVerifyCode(@Validated @RequestBody VerifyCodeReqDTO verifyCodeReqDTO) {
        return Result.success(userService.checkVerifyCode(verifyCodeReqDTO));
    }

    @ApiOperation("发送图片验证码")
    @MustLogin(value = false)
    @GetMapping("/sendPicVerifyCode")
    public Result<Boolean> sendPicVerifyCode(@RequestParam("mobile") String mobile, @RequestParam("key") String key, HttpServletResponse response) {
        return Result.success(userService.sendPicVerifyCode(mobile, key, response));
    }

    @ApiOperation("图片验证码校验")
    @GetMapping("/checkPicVerifyCode")
    @MustLogin(value = false)
    public Result<Boolean> checkPicVerifyCode(@RequestParam("mobile") String mobile, @RequestParam("key") String key, @RequestParam("code") String code) {
        return Result.success(userService.checkPicVerifyCode(mobile, key, code));
    }

    @ApiOperation("密码重置")
    @PostMapping("/reset")
    public Result<Boolean> reset(@RequestBody UserResetPwdReqDTO userResetPwdReqDTO) {
        return Result.success(userService.resetPassword(userResetPwdReqDTO));
    }

    @ApiOperation("用户详情")
    @PostMapping("/info")
    public Result<UserInfoDTO> getUserInfo(@RequestBody UserInfoReqDTO userInfoReqDTO) {
        return Result.success(userService.info(userInfoReqDTO));
    }

    @ApiOperation("Token换取用户详情")
    @PostMapping("/userInfoByToken")
    public Result<UserInfoDTO> userInfoByToken(HttpServletRequest httpServletRequest) {
        String userCode = LoginUserContext.getUserCode();
        System.out.println("当前登录用户信息为---------------"+userCode);
        return Result.success(userService.userInfoByToken(httpServletRequest));
    }
}
